<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Qa;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class QuestionController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('常见问题管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Qa::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('问题ID')->sortable();
            $grid->question('问题')->editable();
            $grid->gender('性别')->display(function ($data) {
                return $data == -1 ? '不限' : ($data == 1 ? '男' : '女');
            });
            $grid->answer('答案')->editable();
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '启用' : '禁用';
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            //$grid->disableActions();
            $grid->filter(function ($filter) {
                $filter->like('question', '问题');
                $filter->like('answer', '答案');
                $filter->equal('flag', '使用状态')->select([-1 => '全部', 0 => '禁用', 1 => '启用']);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(Qa::class, function (Form $form) use ($mode, $id) {
            $form->text('question', '问题');
            $form->textarea('answer', '答案');
            $form->select('gender', '性别')->options([-1 => '不限', 1 => '男', 0 => '女']);
            $form->select('flag', '是否开启')->options([0 => '禁用', 1 => '启用']);
        });
    }
}
